<?php

class FunctionController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// page action renders "static" pages stored under 'protected/views/admin/pages'
			// They can be accessed via: index.php?r=admin/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/admin/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('index');
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}

	/**
	 * Displays the contact page
	 */
	public function actionForm($id=null)
	{
        $request = Yii::app()->request;
        $function=new FunctionComponent();
        if ($id && !($function = GroupComponent::model()->findByPk($id))) {
            $this->redirect(Yii::app()->baseUrl);
        }
        // Uncomment the following line if AJAX validation is needed
        //$this->performAjaxValidation($function);
        $act = $request->getPost("act");
        if($act)
        {
            $data = $request->getPost('GroupComponent');
            $function->name = $data['name'];
            $function->description = $data['description'];
            if($function->save()) {
                $this->redirect(Yii::app()->baseUrl);
            }
        }

        $this->render('form',array(
            'group'=>$function
        ));
	}
}